﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;

/// <summary>
///UIHelper 的摘要说明
/// </summary>
public class UIHelper
{
	public UIHelper()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}

    public static string GridView1_Sorting(StateBag ViewState,object sender, GridViewSortEventArgs e)
    {        
        e.Cancel = true;
        string lastSort = string.Empty;
        SortDirection lastDirection = SortDirection.Ascending;
        if (ViewState["SortExpression"] != null)
        {
            lastSort = ViewState["SortExpression"].ToString();
        }
        if (ViewState["SortDirection"] != null)
        {
            lastDirection = (SortDirection)Enum.Parse(typeof(SortDirection), ViewState["SortDirection"].ToString());
        }
        ViewState["SortExpression"] = e.SortExpression;
        SortDirection thisDirection = SortDirection.Ascending;

        if (lastSort == e.SortExpression)
        {
            if (lastDirection == SortDirection.Ascending)
            {
                thisDirection = SortDirection.Descending;
            }
        }
        ViewState["SortDirection"] = thisDirection.ToString();

        string direction = thisDirection == (SortDirection.Ascending) ? "asc" : "desc";
        string sql = " order by " + e.SortExpression + " " + direction;
        return sql;        
    }
}
